package com.whfc.common.util;

import com.github.promeg.pinyinhelper.Pinyin;

/**
 * @Description: 汉字转拼音
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/28 14:38
 */
public class PinyinUtil {

    /**
     * 获取拼音
     *
     * @param str
     * @param separator
     * @return
     */
    public static String toPinyin(String str, String separator) {
        return Pinyin.toPinyin(str, separator);
    }

    /**
     * 获取拼音
     *
     * @param str
     * @return
     */
    public static String toPinyin(String str) {
        return Pinyin.toPinyin(str, "");
    }

    /**
     * 获取汉字首字母缩写
     *
     * @param str 字符串
     * @return 首字母缩写
     */
    public static String toPinyinAbridge(String str) {
        char[] chars = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {
            if (Pinyin.isChinese(aChar)) {
                sb.append(Pinyin.toPinyin(aChar).toCharArray()[0]);
            } else {
                sb.append(aChar);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(PinyinUtil.toPinyin("武汉test风潮物联"));
        System.out.println(PinyinUtil.toPinyin("武汉test风潮物联", " "));
        System.out.println(PinyinUtil.toPinyinAbridge("武汉test风潮物联"));
    }
}
